import { defineStore } from 'pinia'

import {getGoodsCategory} from '@/api'

interface AppSate {
  config: Record<string, any>
  categoryList: Record<string, any>[]
}

const useAppStore = defineStore('app',{
  state: (): AppSate => {
    return {
      config: {},
      categoryList: []
    }
  },
  getters: {
    getCategoryList: (state) => state.config.categoryList || [],
    getImageUrl: (state) => (url: string) =>
      url ? `${state.config.domain}${url}` : '',
    getWebsiteConfig: (state) => state.config.website || {},
    getLoginConfig: (state) => state.config.login || {},
    getCopyrightConfig: (state) => state.config.copyright || [],
    getQrcodeConfig: (state) => state.config.qrcode || {},
    getAdminUrl: (state) => state.config.admin_url,
    getSiteStatistics: (state) => state.config.siteStatistics || {}
  },
  actions: {
    getConfig() {
      if (this.config && Object.keys(this.config).length) {
        return Promise.resolve(this.config)
      }
      return new Promise((resolve, reject) => {
        // getConfig()
        //   .then((data) => {
        //     this.config = data
        //     resolve(data)
        //   })
        //   .catch((err) => {
        //     reject(err)
        //   })
      })
    },
    getCategory() {
      if (this.categoryList && this.categoryList.length > 0) {
        return Promise.resolve(this.categoryList)
      }
      // return new Promise((resolve, reject) => {
      //   getGoodsCategory()
      //     .then((data) => {
      //       this.categoryList = data.lists || []
      //       resolve(data.lists)
      //     })
      //     .catch((err) => {
      //       reject(err)
      //     })
      // })
    },
  }
})

export default useAppStore
